var app = new Vue({
    el: '#login',
    data () {
        return {
            sendCode:false,
            title: '登录',
            pageShow: 'login', // login: 登录, register: 注册, forgetPassword: 忘记密码
            codeMsg: '获取验证码',
            countdown: 120,
            timer: null,
            errMsg: '',
            username: '',
            pwd: '',
            twopwd: '',
            code: '',
            isgetcode: false,
            isDisable: true,
        }
    },
    watch: {
        errMsg() {
            setTimeout(function () {
                app.errMsg = ''
            }, 1500);
        },
        pageShow (val, oldVla) {
            if (val === 'register') {
                this.username = ''
                this.pwd = ''
                this.twopwd = ''
                this.code = ''
                this.title = '创建账号'
            } else if (val === 'bindMobile') {
                this.title = '绑定手机号'
            } else if (val === 'forgetPassword') {
                this.title = '忘记密码'
            } else {
                this.title = '登录'
            }

            if (this.timer) {
                clearInterval(this.timer)
                this.timer = null
                this.countdown = 120
                this.codeMsg = '获取验证码'
                this.isDisable = true
            }
        },
        username(val,oldVal){
            if(this.checkMobile()){
                this.sendCode=true
            }else{
                this.sendCode=false
            }
        }
    },
    created () {
        let type = getUrlParam('type') // register(注册) bindMobile（绑定手机） forgetPassword（忘记密码） login（登录）
        if (type) {
            this.pageShow = type
        } else {
            this.pageShow = 'login'
        }
    },
    methods: {
        // 返回
        goback () {
            window.history.go(-1);
        },
        // 手机号码登录
        btnLogin () {
            // 校验
            if (!this.username) {
                this.errMsg = "请输入手机号码！"
                return
            }
            if (!this.checkMobile()) {
                this.errMsg = "手机号码格式不正确！"
                return
            }
            if (!this.pwd) {
                this.errMsg = "请输入密码！"
                return
            }
            Axios.post('account/account/login.do', {
                mobile: this.username,
                password: this.pwd
            }).then(res => {
                if (res.respCode === "S") {
                    this.setAccessToken(res.data)
                    let url = document.referrer
                    if (url.indexOf('member-applyFamily') !== -1) {
                        window.location.href = url
                    } else {
                        window.location.href = "../index.html"
                    }
                } else {
                    if (res.errorCode === "2000") {
                        this.errMsg = "账号/密码错误！"
                    } else if (res.errorCode === "1914") {
                        this.errMsg = "用户名不存在，请先注册！"
                    } else {
                        this.errMsg = res.errorMsg
                    }
                }
            }).catch(err => {
                console.error(err)
            })
        },
        // 微信账号登录
        btnWeChat () {
            let host = location.host
            let url = document.referrer
            let data = null
            if (url.indexOf('member-applyFamily') !== -1) {
                data = {
                    redirectUrl: url
                }
            } else {
                data = {
                    redirectUrl: 'http://' + host
                }
            }
            Axios.post('user/weixin/getAuthUrl.do', data).then(res => {
                window.location.href = res.data
            }).catch(err => {
                console.error(err)
            })
        },
        // 获取验证码
        getCode () {
            if (!this.isDisable) return   // 节流
            this.isDisable = false
            // 校验
            if (!this.username) {
                this.errMsg = "请输入手机号码！"
                this.isDisable = true
                return
            }
            if (!this.checkMobile()) {
                this.errMsg = "手机号码格式不正确！"
                this.isDisable = true
                return
            }
            this.sendCode=false
            if (this.countdown !== 120) return
            Axios.post('account/account/getValidCode.do', {
                mobile: this.username
            }).then(res => {
                if (res.respCode === "S") {
                    this.timer = setInterval(() => {
                        this.codeMsg = `重新发送(${this.countdown})`
                        this.countdown--
                        if (this.countdown === 0) {
                            clearInterval(this.timer)
                            this.timer = null
                            this.countdown = 120
                            this.codeMsg = '获取验证码'
                            this.isDisable = true
                            this.isgetcode = false

                            if (this.checkMobile()) {
                                this.sendCode = true
                            }
                        }
                    }, 1000)
                    this.isgetcode = true
                }
            }).catch(err => {
                console.error(err)
            })
        },
        clickAgree () {
            this.errMsg = "最终解释权归邻阿姨科技(深圳)有限公司所有"
        },
        // 注册提交
        submit () {
            // 校验
            if (!this.username) {
                this.errMsg = "请输入手机号码！"
                return
            }
            if (!this.checkMobile()) {
                this.errMsg = "手机号码格式不正确！"
                return
            }
            if (!this.isgetcode) {
                this.errMsg = "请先获取短信验证码！"
                return
            }
            if (!this.code) {
                this.errMsg = "请输入短信验证码！"
                return
            }
            if (!this.pwd && this.pageShow !== 'bindMobile') {
                this.errMsg = "请输入密码！"
                return
            }
            if (!this.checkPassword(this.pwd) && this.pageShow !== 'bindMobile') {
                this.errMsg = "密码格式不正确"
                return
            }
            if (!this.twopwd && this.pageShow !== 'bindMobile') {
                this.errMsg = "请再次输入密码！"
                return
            }
            if (this.pwd !== this.twopwd && this.pageShow !== 'bindMobile') {
                this.errMsg = "两次输入的密码不一致"
                return
            }
            if (this.pageShow === 'bindMobile') {
                this.bindMobile()
            } else if (this.pageShow === 'register') {
                this.register()
            } else {
                this.forgetPassword()
            }
        },
        setAccessToken (key) {
            let domain = ''
            if (window.location.href.indexOf('laykj.cn') > -1) {
                domain = '.laykj.cn'
            } else {
                domain = document.domain
            }
            setCookie('accessToken', key, 7, domain)
        },
        bindMobile () {
            let url = document.referrer
            let accessToken = getUrlParam('accessToken')
            Axios.defaults.headers.common['accessToken'] = accessToken;
            this.setAccessToken(accessToken)
            Axios.post('account/account/bindMobile.do', {
                mobile: this.username,
                validCode: this.code,
            }).then(res => {
                if (res.respCode === "S") {
                    this.errMsg = "手机号绑定成功"
                    this.isgetcode = false
                    Axios.defaults.headers.common['accessToken'] = res.data;
                    this.setAccessToken(res.data)

                    setTimeout(()=>{
                        if (url.indexOf('member-applyFamily') !== -1) {
                            window.location.href = url
                        } else {
                            window.location.href = "../index.html"
                        }
                    },1000)
                } else {
                    this.errMsg = res.errorMsg
                }
            }).catch(err => {
                console.error(err)
            })
        },
        register () {
            Axios.post('account/account/regist.do', {
                mobile: this.username,
                code: this.code,
                password: this.pwd
            }).then(res => {
                if (res.respCode === "S") {
                    this.errMsg = "注册成功！即将跳转登录"
                    this.isgetcode = false
                    setTimeout(() => {
                        this.pageShow = 'login'
                    }, 500)
                } else {
                    if (res.errorCode === '1904') {
                        this.errMsg = '该手机号已注册'
                    } else {
                        this.errMsg = res.errorMsg
                    }
                }
            }).catch(err => {
                console.error(err)
            })
        },
        forgetPassword () {
            Axios.post('account/account/resetPsw.do', {
                mobile: this.username,
                code: this.code,
                password: this.pwd
            }).then(res => {
                if (res.respCode === "S") {
                    this.errMsg = "密码已修改成功！即将跳转登录"
                    this.isgetcode = false
                    setTimeout(() => {
                        this.pageShow = "login"
                    }, 1000)
                } else {
                    this.errMsg = res.errorMsg
                }
            }).catch(err => {
                console.error(err)
            })
        },
        // 验证手机号码
        checkMobile () {
            const reg = /^((1[3-9][0-9])+\d{8})$/
            return reg.test(this.username)
        },
        // 验证密码
        checkPassword (key) {
            const reg = /^(?=.*?[A-Za-z])(?=.*\d)[A-Za-z\d_@\$\*\.]{6,20}$/
            return reg.test(key)
        }
    }
})
